local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local GeneralTool=_G.CommonOHOH.load.GeneralTool();
local InstanceManager = ServerOHOH.load.InstanceManager();
local game_cfg=_G.CommonOHOH.config.Game
local gLevelCount=game_cfg.ChapterLevelCount --每章有多少关
local SceneData={}

function  AddScene(model)
    local sId=tonumber(model.Name)
    if SceneData[sId] then
        return 
    end

    SceneData[sId]=model

    for i=1,gLevelCount do
        local path=string.format("LevelTouch.%s.Touch",i) --碰撞体路径

        local touch=ServerOHOH:GetPath_Loop(path,model)
        touch.Touched:Connect(function(otherPart)
             local playerInst=GeneralTool.TouchPlayerEvent(otherPart)
             if playerInst then
                local player= InstanceManager:FindObject(playerInst)
                player:GetComponent(_G.CommonOHOH.load.Common_LevelBattlCtrl()):EnterLevel(sId,i)
             end
        end)

    end
end

local SceneFolder=workspace:WaitForChild("Scene")
SceneFolder.ChildAdded:Connect(function(child)
        AddScene(child)
end)

for _,v in pairs(SceneFolder:GetChildren()) do
    AddScene(v)
end


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=self.parent
end;

function M.dtor(self)
end;


function M.isCanEnterLevel(self,levelId)
    return true --临时测试
end


function M.EnterLevel(self,sid,index)
    local levelId=gLevelCount*(sid-1)+index
    if self:isCanEnterLevel(levelId) then
        self.player:GetComponent(_G.CommonOHOH.load.Common_TrackCtrl()):CreateLevelTrack(levelId)
    end
end


return M